export default {
  data() {
    return {
      selections: [],
      currentData: null,
      searchData: {},
      autoSearch: true
    }
  },
  methods: {
    search(refresh = false) {
      this.$refs.table.search(refresh)
    },
    handleSearch(data) {
      this.searchData = data
      this.$nextTick(() => {
        this.search(true)
      })
    },
    handleSelectionChange(rows) {
      this.selections = rows
    },
    handleCurrentChange(row) {
      this.currentData = row
    },
    handleAdd() {
      this.$refs.edit.show()
    },
    handleEdit(row) {
      this.$refs.edit.show(row || this.selections[0])
    },
    async handleRemove(row) {
      try {
        await this.$confirm(
          this.$t('tip.removeTip'),
          this.$t('tip.systemTip'),
          {
            type: 'warning'
          }
        )
        const rows = row ? [row] : this.selections
        const ids = rows.map(item => item.id)
        const removeService = this.services.remove
        await removeService(ids)
        this.search()
      } catch { }
    },
    async handleDownload() {
      try {
        await this.services.download(this.searchData)
      } catch { }
    },
    async handleImport(data) {
      const { id: fileId } = data
      try {
        await this.services.import({ fileId })
        this.search()
      } catch { }
    }
  },
  mounted() {
    this.autoSearch && this.search()
  }
}
